﻿package com.asgamer.basics1
{

	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;

	public class SmokeCloud extends MovieClip
	{

		private var stageRef:Stage;
		private var fadeSpeed:Number = .05;
		private var smokeSpeed:Number = 5;
		
		public function SmokeCloud(stageRef:Stage, x:Number, y:Number)
		{
			this.stageRef = stageRef;
			//drop the smoke directly behind our ship.
			this.x = x;
			this.y = y + 19;
			
			//start the smoke a little transparent and small.
			alpha = 0.5 + Math.random()*0.2;
			scaleX = scaleY = 0.3 + Math.random()*0.1;
			
			//rotate the smoke so they don't look the same
			rotation = Math.random() * 360;
			
			addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
		}
		
		public function loop(e:Event) : void 
		{
			//fade out the smoke
			alpha -= fadeSpeed;
			//move the smoke back
			y += smokeSpeed;
			//make the smoke grow in size
			scaleX = scaleY += .07;
			
			//if our smoke is no longer visible remove it
			if (alpha <  0) 
				removeSelf();
		}
		
		private function removeSelf() : void {
			
			if (stageRef.contains(this) )
				stageRef.removeChild(this);
		}
		
	}
	
}